<template>
  <el-dialog
    :title="!this.dataForm.spId ? '新增' : '修改'"
    :close-on-click-modal="false"
    width="90%"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"  label-width="80px">
    <el-form-item label="商品类型" prop="spType">
      <el-input v-model="dataForm.spType" placeholder="商品类型"></el-input>
    </el-form-item>
    <!-- <el-form-item label="商品子类别（SKU）" prop="spChildType">
      <el-input v-model="dataForm.spChildType" placeholder="商品子类别（SKU）"></el-input>
    </el-form-item> -->
    <el-form-item label="商品仓库" prop="spStock">
  <el-input-number  v-model="dataForm.spStock" :min="1"></el-input-number>
      <!-- <el-input v-model="dataForm.spStock" placeholder="商品仓库"></el-input> -->
    </el-form-item>
    <el-form-item label="商品进价" prop="spPurchasePrice">
   <el-input-number  v-model="dataForm.spPurchasePrice" :min="1"></el-input-number>
    </el-form-item>
    <el-form-item label="商品售价" prop="spSellingPrice">
     <el-input-number  v-model="dataForm.spSellingPrice" :min="1"></el-input-number>
    </el-form-item>
    <el-form-item label="分类id" prop="stId">
      <el-input v-model="dataForm.stId" placeholder="分类id"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="spName">
      <el-input v-model="dataForm.spName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品图片" prop="spImg">
     <singleupload v-model="dataForm.spImg"/>
      <!-- <el-input v-model="dataForm.spImg" placeholder="商品的图片"></el-input> -->
    </el-form-item>
    <el-form-item label="销量" prop="spSales">
     <el-input-number  v-model="dataForm.spSales" :min="1"></el-input-number>
    </el-form-item>
    <el-form-item label="商品秒杀价" prop="spPriceSpike">
    <el-input-number  v-model="dataForm.spPriceSpike" :min="1"></el-input-number>
    </el-form-item>
    <el-form-item label="秒杀剩余数" prop="spSpikeCks">
    <el-input-number  v-model="dataForm.spSpikeCks" :min="1"></el-input-number>
    </el-form-item>
    <el-form-item label="热门推荐" prop="spRm">
    <el-switch
            v-model="dataForm.spRm"
            active-color="#13ce66"
            :active-value="1"
            :inactive-value="0"
            inactive-color="#ff4949"
          >
          </el-switch>
      <!-- <el-input v-model="dataForm.spRm" placeholder="是否是热门推荐"></el-input> -->
    </el-form-item>
    <el-form-item label="创建时间" prop="spCreateTime">
      <el-input disabled v-model="dataForm.spCreateTime" placeholder="商品创建时间"></el-input>
    </el-form-item>
    <el-form-item label="商品详情" prop="spText">
  <edit v-model="dataForm.spText"></edit>
      <!-- <el-input v-model="dataForm.spText" placeholder="商品详情"></el-input> -->
    </el-form-item> 
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import showdown from 'showdown'
import edit from '@/components/edit/edit'
import singleupload from "@/components/upload/singleUpload.vue";
    import api from "@/api/modules/cddshop";
  export default {
    components:{
      singleupload,edit
    },
    data () {
      return {
        visible: false,
        dataForm: {
          spId: 0,
          spType: '',
          spChildType: '',
          spStock: '',
          spPurchasePrice: '',
          spSellingPrice: '',
          stId: '',
          spName: '',
          spImg: '',
          spSales: '',
          spPriceSpike: '',
          spSpikeCks: '',
          spRm: '',
          spCreateTime: '',
          spText: ''
        },
        dataRule: {
          spType: [
            { required: true, message: '商品类型不能为空', trigger: 'blur' }
          ],
          spChildType: [
            { required: true, message: '商品子类别（SKU）不能为空', trigger: 'blur' }
          ],
          spStock: [
            { required: true, message: '商品仓库不能为空', trigger: 'blur' }
          ],
          spPurchasePrice: [
            { required: true, message: '商品的进价不能为空', trigger: 'blur' }
          ],
          spSellingPrice: [
            { required: true, message: '商品售价不能为空', trigger: 'blur' }
          ],
          stId: [
            { required: true, message: '分类id不能为空', trigger: 'blur' }
          ],
          spName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          spImg: [
            { required: true, message: '商品的图片不能为空', trigger: 'blur' }
          ],
          spSales: [
            { required: true, message: '销量不能为空', trigger: 'blur' }
          ],
          spPriceSpike: [
            { required: true, message: '商品秒杀价不能为空', trigger: 'blur' }
          ],
          spSpikeCks: [
            { required: true, message: '秒杀剩余数不能为空', trigger: 'blur' }
          ],
          spRm: [
            { required: true, message: '是否是热门推荐不能为空', trigger: 'blur' }
          ],
          
          spText: [
            { required: true, message: '商品详情不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.spId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          api.info(this.dataForm.spId).then(({data}) => {
              if (data) {
                this.dataForm.spType = data.cddShop.spType
                this.dataForm.spChildType = data.cddShop.spChildType
                this.dataForm.spStock = data.cddShop.spStock
                this.dataForm.spPurchasePrice = data.cddShop.spPurchasePrice
                this.dataForm.spSellingPrice = data.cddShop.spSellingPrice
                this.dataForm.stId = data.cddShop.stId
                this.dataForm.spName = data.cddShop.spName
                this.dataForm.spImg = data.cddShop.spImg
                this.dataForm.spSales = data.cddShop.spSales
                this.dataForm.spPriceSpike = data.cddShop.spPriceSpike
                this.dataForm.spSpikeCks = data.cddShop.spSpikeCks
                this.dataForm.spRm = data.cddShop.spRm
                this.dataForm.spCreateTime = data.cddShop.spCreateTime
                this.dataForm.spText = data.cddShop.spText
              }
            })
        })
      },
      // 表单提交
      dataFormSubmit () {
        let converter = new showdown.Converter()// 初始化转换器
 this.dataForm.spText= converter.makeHtml( this.dataForm.spText)// 将 markdown 语法的字符串转换为 html
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
    var parms= {
'spId': this.dataForm.spId || undefined,
'spType': this.dataForm.spType,
'spChildType': this.dataForm.spChildType,
'spStock': this.dataForm.spStock,
'spPurchasePrice': this.dataForm.spPurchasePrice,
'spSellingPrice': this.dataForm.spSellingPrice,
'stId': this.dataForm.stId,
'spName': this.dataForm.spName,
'spImg': this.dataForm.spImg,
'spSales': this.dataForm.spSales,
'spPriceSpike': this.dataForm.spPriceSpike,
'spSpikeCks': this.dataForm.spSpikeCks,
'spRm': this.dataForm.spRm,
'spCreateTime': this.dataForm.spCreateTime,
'spText': this.dataForm.spText        }
        api.updateAdd(parms,this.dataForm.spId).then((res) => {
              if (res.code==200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
